module.exports = function($scope, $modal, officeList, officeInfo, officeSave, 
						  delOffice, areaList, addOffice, addUser, roleList){

	$scope.what = 'list';		//查看状态 modify:修改机构,list:机构列表 add:添加机构
	$scope.obj = {};			//机构对象
	$scope.status = {
		'active' : true,
		'flag': false
	};

	init();
	initArea();
	//初始化机构列表
	function init(){
		$scope.status.active = true;
		$scope.tabname = { 'name': '添加机构', 'active': false };
		$("#treetable").hide();
		officeList.save({}, function(res){
			if (res.errcode === 0) {
				$scope.office_list = res.data;
				//将登录人信息放在第一个机构信息内,作为判断添加机构或运营商时使用
				$scope.office_list[0].parent_id = '0';
				if($scope.office_list[0].uid == '1'){
					$scope.tabname = { 'name': '添加运营商', 'active': false };
				}
			} else {
				alert(res.errmsg);
			}
		});
	}

	//初始化区域列表
	function initArea(){
		areaList.save({}, function(res){
			if (res.errcode === 0) {
				$scope.cityList = res.data;
			} else {
				alert(res.errmsg);
			}
		});
	}

	//修改界面
	$scope.update = function (item) {
		if(item.flag != 1){
			return;
		}
		$scope.tabname = { 'name': '修改机构', 'active': true };
		officeInfo.save(item, function (res) {
			if (res.errcode === 0) {
				$scope.obj = res.data;
				if($scope.obj.sys_area == '' || $scope.obj.sys_area == undefined){
					$scope.status.flag = false;
				}else{
					$scope.status.flag = true;
				}
			} else {
				alert(res.errmsg);
			}
		});
	};

    //保存机构
    $scope.save = function(){
		if(!$scope.obj.id){
			$scope.obj.rolelist = []
			for(var i = 0; i < $scope.role_list.length; i++){
				if($scope.role_list[i].active){
					$scope.obj.rolelist.push($scope.role_list[i]);
				}
			}
		}
		if($scope.obj.parent_id == '0'){
			//最高管理员添加运营商执行
			addOffice.save($scope.obj, function(res){
				if(res.errcode === 0){
					init();
				}else if(res.errcode === 1106){
					alert('机构编码已存在!');
				}else{
					alert(res.errmsg);
				}
			});
		} else {
			officeSave.save($scope.obj, function(res){
				if(res.errcode === 0){
					init();
				}else{
					alert(res.errmsg);
				}
			});
		}
    };

    //取消=重置
    $scope.cancel = function(){
		if($scope.office_list[0].admin == '1'){
			$scope.tabname = { 'name': '添加运营商', 'active': false };
		} else {
			$scope.tabname = { 'name': '添加机构', 'active': false };
		}
		$scope.status.active = true;
		$scope.obj = {};
    };

	//删除
    $scope.delete = function(item){
		if(confirm('确认要删除该用户吗？\r\n此操作会删除机构下全部用户')==true){
			delOffice.save(item, function(res){
				if(res.errcode === 0){
					init();
				}else{
					alert(err.errmsg);
				}
			});
		}
    };

	//添加界面---flag=0时表示点击的时页面标签进入此方法,否则是点击添加下级进入
	$scope.add = function (item,flag) {
		if($scope.tabname.name == '修改机构'){
			return;
		}
		$scope.obj = {};
		var sort = '';
		if(item.admin == '1' && flag == 0){
			//一定是点击标签页的添加运营商才会进入这里
			$scope.status.flag = false;
			$scope.obj.parent_id = '0';
			$scope.obj.grade = '1';
			sort = $("#treetable").find("tr[pid='0']:last").attr("sort");
		} else {
			//点击机构后面的添加下级,进入这里
			if(item.sys_area == '' || item.sys_area == undefined || item.sys_area == null){
				alert('无发行区域,不可添加下级');
				return;
			}
			$scope.obj.addSub = true;
			$scope.obj.super_name = item.name;
			$scope.obj.parent_id = item.id;
			$scope.obj.area_id = item.area_id;
			$scope.obj.grade = parseInt(item.grade)+1+'';
			sort = $("#treetable").find("tr[pid="+item.id+"]:last").attr("sort");
			$scope.obj.sys_area = item.sys_area;
			$scope.status.flag = true;
		}
		if(sort == undefined){
			sort = '0';
		}
		$scope.tabname.active = true;
		$scope.obj.USEABLE = '1';
		$scope.obj.type = '1';
		$scope.obj.sort = parseInt(sort)+30;
		//点击添加机构时加载角色列表
		$scope.loadrolelist();
	};

	//设置景区
    $scope.addView = function(office){
		var modalInstance = $modal.open({
			template: require('../views/addview.html'),
			controller: 'addView',
			size: 'lg',
			resolve: {
				item: function () {
					return office;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function() {
  			init();
		});
	};


	//加载角色列表-创建机构同名用户时选择角色
	$scope.loadrolelist = function () {
		roleList.save({}, function (res) {
			if (res.errcode === 0) {
				$scope.role_list = res.data;
			} else {
				alert(res.errmsg);
			}
		});
	};


};